﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Storage;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

namespace Janus.Cores.Configuration
{
    /// <summary>
    /// 
    /// </summary>
    [ConfigurationElementType(typeof(FileConfigElement))]
    public class FileConfigSource:Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="configurationFilepath"></param>
        public FileConfigSource(string configurationFilepath)
            : this(configurationFilepath, true)
        {

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="configurationFilepath"></param>
        /// <param name="referesh"></param>
        public FileConfigSource(string configurationFilepath, bool referesh)
            : this(configurationFilepath, referesh, 15000)
        {

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="configurationFilepath"></param>
        /// <param name="referesh"></param>
        /// <param name="refreshInterval"></param>
        public FileConfigSource(string configurationFilepath, bool referesh, int refreshInterval) :
            base(RetriveActualFilePath(configurationFilepath), referesh, refreshInterval)
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        static string RetriveActualFilePath(string file)
        {
            if( !Directory.Exists(file) )
                file = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, file);
            return file;
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public class FileConfigElement : FileConfigurationSourceElement
    {
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override IConfigurationSource CreateSource()
        {
            IConfigurationSource createdObject = new FileConfigSource(FilePath);

            return createdObject;
        }

    }
}
